package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.MilestoneTask;
import com.ruoyi.system.domain.Task;
import com.ruoyi.system.mapper.MilestoneTaskMapper;
import com.ruoyi.system.mapper.TaskMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MilestoneMapper;
import com.ruoyi.system.domain.Milestone;
import com.ruoyi.system.service.IMilestoneService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-14
 */
@Service
public class MilestoneServiceImpl implements IMilestoneService 
{
    @Autowired
    private MilestoneMapper milestoneMapper;

    @Autowired
    private MilestoneTaskMapper milestoneTaskMapper;

    @Autowired
    private TaskMapper taskMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Milestone selectMilestoneById(Long id)
    {
        Milestone milestone = milestoneMapper.selectMilestoneById(id);
        setOther(id, milestone);
        return milestone;
    }

    private void setOther(Long id, Milestone milestone) {
        //查询中间表
        List<MilestoneTask> milestoneTasks = milestoneTaskMapper.selectMilestoneTaskByMilestoneId(id);
        List<Task> reult = new ArrayList<>();
        //查询任务表
        for (MilestoneTask milestoneTask : milestoneTasks) {
            Task task = taskMapper.selectTaskByIdAndMilestoneId(milestoneTask.getTaskId(),id);
            reult.add(task);
        }
        milestone.setTasksList(reult);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param milestone 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Milestone> selectMilestoneList(Milestone milestone)
    {
        List<Milestone> milestones = milestoneMapper.selectMilestoneList(milestone);
        for(Milestone milestoneDB:milestones){
            setOther(milestoneDB.getId(), milestoneDB);
        }
        return milestones;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param milestone 【请填写功能名称】
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMilestone(Milestone milestone)
    {
        int i = milestoneMapper.insertMilestone(milestone);
        List<Task> tasksList = milestone.getTasksList();
        for (Task task : tasksList) {
            //插入中间表
            MilestoneTask milestoneTask = new MilestoneTask();
            milestoneTask.setMilestoneId(milestone.getId());
            milestoneTask.setTaskId(task.getId());
            try {
                milestoneTaskMapper.insertMilestoneTask(milestoneTask);
            } catch (Exception e) {
                throw new RuntimeException("插入中间表失败",e);
            }
        }
        return i;
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param milestone 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateMilestone(Milestone milestone)
    {
        //查询数据库，有差的中间表则补，多的则删除
        Milestone milestoneDB = selectMilestoneById(milestone.getId());
        List<Task> tasksList = milestone.getTasksList();
        List<Task> tasksListDB = milestoneDB.getTasksList();
        for (Task task : tasksList) {
            boolean exist = false;
            for (Task taskDB : tasksListDB) {
                if(task.getId().equals(taskDB.getId())){
                    exist = true;
                    break;
                }
            }

            if(!exist){
                //插入中间表
                MilestoneTask milestoneTask = new MilestoneTask();
                milestoneTask.setMilestoneId(milestone.getId());
                milestoneTask.setTaskId(task.getId());
                try {
                    milestoneTaskMapper.insertMilestoneTask(milestoneTask);
                } catch (Exception e) {
                    throw new RuntimeException("插入中间表失败",e);
                }
            }
        }

        //查找删除的
        for (Task taskDB : tasksListDB) {
            boolean exist = false;
            for (Task task : tasksList) {
                if(taskDB.getId().equals(task.getId())){
                    exist = true;
                    break;
                }
            }
            if(!exist){
                //删除中间表
                MilestoneTask milestoneTask = new MilestoneTask();
                milestoneTask.setMilestoneId(milestone.getId());
                milestoneTask.setTaskId(taskDB.getId());
                try {
                    milestoneTaskMapper.deleteMilestoneTaskByMilestoneIdAndTaskId(milestoneTask);
                } catch (Exception e) {
                    throw new RuntimeException("删除中间表失败",e);
                }
            }
        }

        return milestoneMapper.updateMilestone(milestone);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMilestoneByIds(Long[] ids)
    {
        return milestoneMapper.deleteMilestoneByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteMilestoneById(Long id)
    {
        int i = milestoneMapper.deleteMilestoneById(id);
        if (i>0){
            //删除中间表
            milestoneTaskMapper.deleteMilestoneTaskByMilestoneId(id);
        }else {
            throw new RuntimeException("删除失败");
        }
        return i;
    }

    @Override
    public void checkTaskMilestone(Long[] tasks) {
        for (Long task : tasks) {
            MilestoneTask milestoneTask = milestoneTaskMapper.selectMilestoneTaskByTaskId(task);
            if(milestoneTask != null){
                throw new RuntimeException("任务" + task +"已关联里程碑，请先解除关联");
            }
        }
    }
}
